package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.triones.ljr.entity.dto.PolicyOptionDataDO;
import com.triones.ljr.entity.vo.PolicyOptionCategoryVO;
import com.triones.ljr.entity.vo.PolicyOptionDataVO;
import com.triones.ljr.service.PolicyOptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 政策选项
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/policyOption")
public class PolicyOptionController extends BaseController {

    private final PolicyOptionService policyOptionService;

    /**
     * 分类-查询列表（分页）
     */
    @SaCheckPermission("system:policyOption:pageList")
    @GetMapping("/category/pageList")
    public TableDataInfo<PolicyOptionCategoryVO> categoryPageList(PageQuery pageQuery) {
        return policyOptionService.categoryPageList(pageQuery);
    }

    /**
     * 获取选择框列表
     */
    @GetMapping("/category/optionSelect")
    public R<List<PolicyOptionCategoryVO>> optionSelect() {
        List<PolicyOptionCategoryVO> list = policyOptionService.selectCategoryAll();
        return R.ok(list);
    }

    /**
     * 分类-查询详细
     * @param id
     */
    @GetMapping(value = "/category/{id}")
    public R<PolicyOptionCategoryVO> getCategoryInfo(@PathVariable Long id) {
        return R.ok(policyOptionService.getCategoryInfo(id));
    }

    /**
     * 查询列表（分页）
     */
    @SaCheckPermission("system:policyOption:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<PolicyOptionDataVO> pageList(PolicyOptionDataDO dto, PageQuery pageQuery) {
        return policyOptionService.pageList(dto, pageQuery);
    }

    /**
     * 查询详细
     * @param id
     */
    @GetMapping(value = "/{id}")
    public R<PolicyOptionDataVO> getInfo(@PathVariable Long id) {
        return R.ok(policyOptionService.getInfo(id));
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:policyOption:add")
    @Log(title = "政策选项", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody PolicyOptionDataDO dto) {
        if (!policyOptionService.checkNameUnique(dto)) {
            return R.fail("新增选项'" + dto.getName() + "'失败，应用名称已存在");
        }
        return toAjax(policyOptionService.add(dto));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:policyOption:edit")
    @Log(title = "政策选项", businessType = BusinessType.INSERT)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody PolicyOptionDataDO dto) {
        if (!policyOptionService.checkNameUnique(dto)) {
            return R.fail("修改应用'" + dto.getName() + "'失败，应用名称已存在");
        }
        return toAjax(policyOptionService.edit(dto));
    }

    /**
     * 删除
     * @param id
     */
    @SaCheckPermission("system:policyOption:remove")
    @Log(title = "政策选项", businessType = BusinessType.INSERT)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(policyOptionService.remove(id));
    }

    /**
     * 获取政策选项数据
     * 政策级别：1934794008470931234(政策收集)
     * 政策类别：1934794008470931235(政策收集)
     * 发文机关：1934794008470931236(政策收集)
     * 政策级别：1934794008470931237(梁平惠企政策超市)
     * 政策类别：1934794008470931238(梁平惠企政策超市)
     * 发文机关：1934794008470931239(梁平惠企政策超市)
     */
    @GetMapping("/list/{cid}")
    public R<List<PolicyOptionDataVO>> optionDataList(@PathVariable Long cid) {
        return R.ok(policyOptionService.optionDataListByCid(cid));
    }
}
